package com.android.vspice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;

import com.android.vspice.swig.Element;
import com.android.vspice.swig.VSpice;

public class RecogActivity extends Activity {
	byte[] back = null;
	ElementList elements;
	View viewButton;
	ImageButton btnAnalysis;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		elements = ElementList.getInstance();
		Element[] e;
		
		disableScreenTurnOff();
        setOrientation();
        setFullscreen();
        
		Intent i = getIntent();
		back = i.getExtras().getByteArray("image");
		RecogView recog = new RecogView(this, back);
		
		setContentView(recog);
		
		LayoutInflater inflater = getLayoutInflater();
	    viewButton = inflater.inflate(R.layout.recog, null);
	    addContentView(viewButton, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	    
	    btnAnalysis = (ImageButton)findViewById(R.id.button_next);
	    btnAnalysis.setOnClickListener(listenNext);
	}

	/**
	 * Avoid that the screen get's turned off by the system.
	 */
	public void disableScreenTurnOff() {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/**
	 * Set's the orientation to landscape, as this is needed by AndAR.
	 */
	public void setOrientation() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * Maximize the application.
	 */
	public void setFullscreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void setNoTitle() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	OnClickListener listenNext = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(RecogActivity.this, 
					AnalysisSetting.class);
			startActivity(i);
		}
		
	};
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_UP) {
			float x = event.getX();
			float y = event.getY();
			
			// touch any element
			
			Intent i = new Intent(RecogActivity.this, ElementSetting.class);
			i.putExtra("index", 0);
			startActivity(i);
			return true;	
		}
			
		return super.onTouchEvent(event);
	}
	
	private class RecogView extends View {
		Bitmap bitBack = null;
		List<Pair<Point, Integer>> listCircle = 
			new ArrayList<Pair<Point, Integer>>();
		List<Pair<Point, Point>> listLine = 
			new ArrayList<Pair<Point, Point>>();
		
		public RecogView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		public RecogView(Context context, byte[] data)
		{
			super(context);
			if(data == null)
				return;
			
			int[] pixels = new int[VisionSpice.WIDTH * VisionSpice.HEIGHT];
			int offset = 0;
			for(int i = 0 ; i < VisionSpice.HEIGHT ; i++) {
				for(int j = 0 ; j < VisionSpice.WIDTH ; j++) {
					pixels[i * VisionSpice.WIDTH + j] = 
						0xff000000 |
						(data[offset++] & 0xff) << 16 |
						(data[offset++] & 0xff) << 8 | 
						(int)data[offset++] & 0xff;
				}
			}
			
			bitBack = Bitmap.createBitmap(pixels, 
					VisionSpice.WIDTH, VisionSpice.HEIGHT, Config.ARGB_8888);
			
		}
		
		public void drawCircle(Point p, int radius) {
			listCircle.add(new Pair<Point, Integer>(p, radius));
			invalidate();
		}
		
		public void drawLine(Point p1, Point p2) {
			listLine.add(new Pair<Point, Point>(p1, p2));
			invalidate();
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			if(bitBack != null) {
				canvas.drawBitmap(bitBack, 
						null, 
						new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), 
						paint);
			}
			
			if(!listCircle.isEmpty()) {
				for(int i = 0, n = listCircle.size() ;
					i < n ; i++) {
					Pair<Point, Integer> p = listCircle.get(i);
					canvas.drawCircle(p.first.x, p.first.y, p.second, paint);
				}
			}
			
			if(!listLine.isEmpty()) {
				for(int i = 0, n = listLine.size() ; 
					i < n ; i++) {
					Pair<Point, Point> p = listLine.get(i);
					canvas.drawLine(p.first.x, 
							p.first.y, p.second.x, p.second.y, paint);
				}
			}
			// TODO Auto-generated method stub
			super.onDraw(canvas);
		}
	}
}
